import { ServerConfig, ServerValues } from "../../../kds-base-define/src/ServerConfig"
import { TeaDefine } from "../../../kds-base-define/src/TeaDefine"
import { DB } from "../db"
import { Rpc } from "../rpc"


let db = DB.get("tds-tea-record","tea-record")
function getTeaRecordTableName(teaID:number,recordName:string) {
	return "t_record_" + recordName + "_" + teaID
}
async function addRecord(h:string,teaID:number,recordName:string,data:any) {
	let tableName = getTeaRecordTableName(teaID,recordName)
	data["serial"] = await Rpc.center.callException("kds.ids.getId",tableName,10000000,1)
	await db.insert(tableName,data)
}

async function getRecord(h:string,teaID:number,recordName:string,opt:TeaDefine.Record.DbpOption) {
	opt = opt || {}
	let kv = opt.kv || {}
	if(opt.times) {
		kv["timestamp"] = {"$gte":opt.times.from,"$lt":opt.times.to}
	}
	return await db.getOption(getTeaRecordTableName(teaID,recordName),kv,{
		limit:opt.limitCount,
		sort:{timestamp:-1}
	})
	// with sort ?
}

export let RpcTeaRecord = {
	add:addRecord,
	get:getRecord,
}